package com.antu.first.web.merchant;

import com.alibaba.fastjson.JSON;
import com.antu.first.bean.Hotel;
import com.antu.first.bean.Merchant;
import com.antu.first.dto.HotelOperationExecution;
import com.antu.first.dto.ImageHolder;
import com.antu.first.exceptions.HotelOperationException;
import com.antu.first.service.HotelService;
import com.antu.first.util.ImageHandle;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/2 0002.
 */
@Controller
@RequestMapping("/hotelController")
public class HotelController {

    @Autowired
    HotelService hotelService;
    Map<String,Object> map = new HashMap<String, Object>();
    private  Hotel hotel;
    boolean flag;
    /**
     * 查询酒店基本信息
     */
    @RequestMapping(value = "/queryHotelByName")
    @ResponseBody
    public Hotel queryHotelByName(HttpServletRequest httpServletRequest){
        Merchant merchant= (Merchant) httpServletRequest.getSession().getAttribute("merchant");//取出对象
        hotel = hotelService.queryHotelByName( merchant.getHotel().gethId());
        return hotel;
    }

    /**
     * 商家修改酒店基本信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateHotelInfo")
    @ResponseBody
    public String updateHotelInfo(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        ImageHolder hotelImg = null;
        try{
            ObjectMapper mapper = new ObjectMapper();
            String hotelStr = request.getParameter("hotelStr");
            Hotel hotel = mapper.readValue(hotelStr, Hotel.class);
            CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getServletContext());
            if(resolver.isMultipart(request)){
                hotelImg = ImageHandle.handleImage(request);
            }
        }catch(Exception e){
            throw new HotelOperationException("HotelController-updateHotelInfo,在处理酒店缩略图信息的过程中发生异常："+e.toString());
        }
        if(hotel != null && hotel.gethId() != 0){
            try{
                HotelOperationExecution execution = hotelService.updateHotelInfo(hotel, hotelImg);
                if(execution.getState() != 0){
                    modelMap.put("success", false);
                    modelMap.put("errMsg", execution.getStateInfo());
                    return JSON.toJSONString(modelMap);
                }
                modelMap.put("success", true);
                modelMap.put("msg", execution.getStateInfo());
            }catch(Exception e){
                throw new HotelOperationException("HotelController-updateHotelInfo,在处理轮播图文字信息的过程中发生异常："+e.toString());
            }
        }else{
            modelMap.put("success", false);
            modelMap.put("errMsg", "请完善您的酒店相关信息后再提交！");
            return JSON.toJSONString(modelMap);
        }
       // System.out.println(hotel);
        return JSON.toJSONString(modelMap);
    }
}
